<!-- Banner管理新增、修改 -->
<template>
  <div>
    <el-dialog :title="BannerData.title" :visible.sync="BannerData.showModal" width="750px" :close-on-click-modal="false" top="5vh">
      <el-form label-width="140px" :model="data" ref="update" :rules="rules" v-loading="showLoading" :element-loading-text="$t('lan.dataLoading')">
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('lan.mainTitle')" prop="code">
              <el-input size="medium" v-model="data.code" :placeholder="$t('lan.pleaseTitle')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('lan.subTitle')" prop="name">
              <el-input size="medium" v-model="data.name" :placeholder="$t('lan.pleaseSub')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('lan.category')" prop="type">
              <el-select v-model="data.type" size="medium" :placeholder="$t('lan.pleaseCategory')">
                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('lan.uploadImg')" prop="url">
              <el-upload class="upload-demo" :action='fileUrl + "/attachment/fileUpload"' name="uploadFile" :headers="tokenimg" :on-success="onSuccess" :before-upload="beforeUpload" :show-file-list="false">
                <el-button size="small" type="primary">{{$t('lan.clickUpload')}}</el-button>
                <div>
                  <img v-if="data.url" :src="data.url" class="imgbox" />
                </div>
                <div slot="tip" class="el-upload__tip">{{$t('lan.only3MB')}}</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('lan.jumpAddress')" prop="targetUrl">
              <el-input size="medium" v-model="data.targetUrl" :placeholder="$t('lan.pleaseJumpAddress')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 底部按钮开始 -->
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" type="primary" @click="save">{{$t('lan.submit')}}</el-button>
        <el-button size="medium" @click="BannerData.showModal = false">{{$t('lan.cancel')}}</el-button>
      </span>
      <!-- 底部按钮结束 -->
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["BannerData"],
  data () {
    // 自定义图片校验
    let valiurl = (rule, value, callback) => {
      if (this.data.url == '') {
        callback(new Error(this.$t('lan.pleaseUpload')));
      } else {
        callback();
      }
    };
    return {
      uploadFile: "", // 图片的文件名称
      tokenimg: { token: sessionStorage.getItem("token") }, // 图片请求头
      //表单校验
      rules: {
        // code: [{ required: true, message: "编号不能为空", trigger: "blur" }],
        // name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        type: [{ required: true, message: this.$t('lan.pleaseCategory'), trigger: "blur" }],
        url: [{ required: true, validator: valiurl, trigger: "change" }]
        // targetUrl: [
        //   { required: true, message: "跳转地址不能为空", trigger: "blur" }
        // ]
      },
      fileUrl: this.request.server,
      showLoading: false, //是否显示loading
      data: {
        code: "", // 参数编号
        name: "", // 参数名称
        type: "", // 类别
        url: "", // 图片地址
        targetUrl: "" // 跳转地址
      },
      typeList: [] //类别列表
    };
  },
  methods: {
    // 图片上传成功后，后台返回图片的路径
    onSuccess (res) {
      if (res.code == 200) {
        this.data.url = res.data;
      }
    },
    // 图片校验
    beforeUpload (file) {
      // const isJPG = file.type === 'image/jpeg'
      var testmsg = /^image\/(jpeg|png|jpg)$/.test(file.type);
      const isLt5KB = file.size / 1024 / 1024 < 2;
      if (!testmsg) {
        this.$message.error(this.$t('lan.uploadFormat'));
        return;
      }
      if (!isLt5KB) {
        this.$message.error(this.$t('lan.uploaded2MB'));
      }
      return testmsg && isLt5KB;
    },
    //获取字典表
    async getterm () {
      const url = '/category/queryList'
      const result = await this.request.httpRequest('POST', url, { pid: 0 });
      if (result.code === 200) {
        this.typeList = result.data
      }
    },
    //新增、修改参数
    save () {
      this.$refs["update"].validate(async valid => {
        if (valid) {
          let url =
            this.BannerData.type == "create"
              ? "/banner/save"
              : "/banner/update";
          let httpMethod = this.BannerData.type == "create" ? "POST" : "PUT";
          let result = await this.request.httpRequest(
            httpMethod,
            url,
            this.data
          );
          if (result.code == 200) {
            this.BannerData.showModal = false;
            this.$message({ type: "success", message: result.msg });
            this.$emit("closeModal");
          } else {
            this.$message({ type: "error", message: result.msg });
          }
        }
      });
      this.showLoading = false;
    },
    //获取详情
    async getDetail () {
      if (this.BannerData.userId) {
        let url = `/banner/get/${this.BannerData.userId}`;
        let result = await this.request.httpRequest("GET", url);
        if (result.code == 200) {
          this.data.id = result.data.id;
          this.data.name = result.data.name;
          this.data.code = result.data.code;
          this.data.type = Number(result.data.type);
          this.data.url = result.data.url;
          this.data.targetUrl = result.data.targetUrl;
        } else {
          this.$message({ type: "error", message: result.msg });
        }
      }
    }
  },
  mounted () {
    this.getterm();
    this.getDetail();
  }
};
</script>
<style scoped>
</style>